
/**
 * 构造方法
 */
function DeliveryOrder(params) {

	var bike20minsOrderAmount = 1
	var bike1hourOrderAmount = 3

	var electricMobile20minsOrderAmount = 2
	var electricMobileOrderAmount = 4


	var car20minsOrderAmount = 3
	var car1hourOrderAmount = 5

	var deliverymans = params.deliverymans;

	var bike = deliverymans.bike;
	var electricMobile = deliverymans.electricMobile;
	var car = deliverymans.car;

	var totalDeliverymans = 0; //总外送员数量
	//this.totalDeliverymans = deliverymans.length; //总外送员数量
	var total20minsOrderAmount = 0; //20分钟总订单量
	var total1hourOrderAmount = 0; //1小时总订单量

	/*for (var i = 0; i < this.totalDeliverymans; i++) {
		if (deliverymans[i] == 'bike') {
			total20minsOrderAmount += 1;
			total1hourOrderAmount += 3;
		} else if (deliverymans[i] == 'electricMobile') {
			total20minsOrderAmount += 2;
			total1hourOrderAmount += 4;
		} else if (deliverymans[i] == 'car') {
			total20minsOrderAmount += 3;
			total1hourOrderAmount += 5;
		}
	}*/

	if (bike) {
		totalDeliverymans += bike;
		total20minsOrderAmount += bike20minsOrderAmount * bike;
		total1hourOrderAmount += bike1hourOrderAmount * bike;
	}

	if (electricMobile) {
		totalDeliverymans += electricMobile;
		total20minsOrderAmount += electricMobile20minsOrderAmount * electricMobile;
		total1hourOrderAmount += electricMobileOrderAmount * electricMobile;
	}

	if (car) {
		totalDeliverymans += car;
		total20minsOrderAmount += car20minsOrderAmount * car;
		total1hourOrderAmount += car1hourOrderAmount * car;
	}

	this.totalDeliverymans = totalDeliverymans;
	this.total20minsOrderAmount = total20minsOrderAmount;
	this.total1hourOrderAmount = total1hourOrderAmount;

}

/**
 * 检验用户设置的外送时间 是否可以提供服务
 * true : 可以 false : 不可以
 */
DeliveryOrder.prototype.checkDeliveryTimeCanService = function(currentTime,currentOrder, allOrders) {

	var deliveryOrders = {}; //处理后订单
	var orderslength = orders.length;

	var orderClock = _getStdTime(currentOrder.orderClock);
	var orderHour = parseInt(orderClock.substr(orderClock.length - 4, 2));
	var orderMinute = parseInt(orderClock.substr(orderClock.length - 2, 2));

	var arriveBeginClock = _getStdTime(currentOrder.arriveBeginClock);
	var arriveEndClock = _getStdTime(currentOrder.arriveEndClock);
	var arriveBeginHour = parseInt(arriveBeginClock.substr(arriveBeginClock.length - 4, 2));
	//var arriveBeginMinute = parseInt(arriveBeginClock.substr(arriveBeginClock.length - 2, 2));
	var arriveEndHour = parseInt(arriveEndClock.substr(arriveEndClock.length - 2, 2));
	//var arriveEndMinute = parseInt(arriveEndClock.substr(arriveEndClock.length - 2, 2));
	var currentHour = parseInt(currentTime.substr(currentTime.length - 4, 2));
	var currentMinute = parseInt(currentTime.substr(currentTime.length - 2, 2));

	if (currentHour > arriveEndHour) {
		return false; // 现在已经超过了 最晚送达时间
	}

	if (currentHour == arriveEndHour && orderMinute > 20) {
		return false; // 如果超过20分钟 只能放到下一个小时
	}

	var i = arriveBeginHour;
	for (; i <= arriveEndHour; i++) {
		if ((allOrders[i] && allOrders[i].length == this.total1hourOrderAmount) 
			|| (_getPeriodOrderCountByOrderClock('0:00', i + ':00', i + ':00', (i + 1) + ':00', allOrders) 
				<= this.total1hourOrderAmount - this.total20minsOrderAmount 
				&& _getPeriodOrderCountByOrderClock(i + ':00', i + ':20', i + ':00', (i + 1) + ':00', allOrders) 
				== this.total20minsOrderAmount)
			) {
			
			continue;
		} else {
			break;
		}
	}

	if(i > arriveEndHour) {
		return false;
	}

	return true;

}

/**
 * 计算外送时间
 * 订单需按时间排序
 */
DeliveryOrder.prototype.calculateDeliveryOrder = function(startHour, orders) {
	var deliveryOrders = {}; //处理后订单
	var orderslength = orders.length;
	for (var i = 0; i < orderslength; i++) {
		if (startHour > 23) {
			break;
		}
		var currentOrder = orders[i]; //当前处理订单
		if ((deliveryOrders[startHour] && deliveryOrders[startHour].length == this.total1hourOrderAmount ) || (_getPeriodOrderCountByOrderClock('0:00', startHour + ':00', startHour + ':00', (startHour + 1) + ':00', deliveryOrders[startHour]) <= this.total1hourOrderAmount - this.total20minsOrderAmount && _getPeriodOrderCountByOrderClock(startHour + ':00', startHour + ':20', startHour + ':00', (startHour + 1) + ':00', deliveryOrders[startHour]) == this.total20minsOrderAmount) || _getStdTime(currentOrder.orderClock) > _getStdTime(startHour + ':20')) {
			startHour++;
		}
		if(!deliveryOrders[startHour]) {
			deliveryOrders[startHour] = [];
		}
		deliveryOrders[startHour].push(currentOrder);
	}
	return deliveryOrders;
}

/**
 * 将冒号分隔的时间字符串转化为标准时间字符串
 */
this._getStdTime = function(timeStr) {
	var times = timeStr.split(":");
	if (times.length != 2) {
		return timeStr;
	} else {
		var hour = times[0] < 10 ? '0' + times[0] : times[0];
		var minute = times[1] < 10 ? '0' + times[1] : times[1];
		return hour + minute;
	}
}

/**
 * 获取某一时间段内的订单数量
 */
this._getPeriodOrderCount = function(startTime, endTime, deliveryOrders) {
	if (!deliveryOrders) {
		return 0;
	}
	startTime = _getStdTime(startTime);
	endTime = _getStdTime(endTime);
	var count = 0;
	for (var i = 0; i < deliveryOrders.length; i++) {
		var arriveBeginClock = _getStdTime(deliveryOrders[i].arriveBeginClock);
		var arriveEndClock = _getStdTime(deliveryOrders[i].arriveEndClock);
		if (startTime <= arriveBeginClock && endTime >= arriveEndClock) {
			// 在统计范围内
			count++;
		}
	}
	return count;
}

/**
 * 获取指定订单确定时间之前 某一时间段内的订单数量
 */
this._getPeriodOrderCountByOrderClock = function(startOrderClock, endOrderClock, startTime, endTime, deliveryOrders) {

	if (!deliveryOrders) {
		return 0;
	}

	startTime = _getStdTime(startTime);
	endTime = _getStdTime(endTime);
	startOrderClock = _getStdTime(startOrderClock);
	endOrderClock = _getStdTime(endOrderClock);

	var count = 0;
	for (var i = 0; i < deliveryOrders.length; i++) {
		var arriveBeginClock = _getStdTime(deliveryOrders[i].arriveBeginClock);
		var arriveEndClock = _getStdTime(deliveryOrders[i].arriveEndClock);
		var orderClock = _getStdTime(deliveryOrders[i].orderClock);
		if (orderClock >= startOrderClock && orderClock <= endOrderClock && startTime <= arriveBeginClock && endTime >= arriveEndClock) {
			// 在统计范围内
			count++;
		}
	}

	return count;
}